package com.sri.controller.property;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.sri.business.property.PropertyManager;
import com.sri.cache.CacheManager;
import com.sri.formBeans.PropertyForm;

public class PropertyAction extends DispatchAction {

	public ActionForward publish(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		PropertyForm propertyForm = (PropertyForm) form;
		PropertyManager.storePropery(propertyForm);
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(2));
		return mapping.findForward("success");
	}

	public ActionForward addProperty(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("stateCities", CacheManager.getCitiesByStateId(2));
		PropertyForm propertyForm = (PropertyForm) form;
		propertyForm.setState("2");
		propertyForm.setPetsAllowed(true);
		propertyForm.setRentNegotiable(true);
		return mapping.findForward("success");
	}

	/**
	 * Method getCities by stateId
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward getCities(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int stateId = Integer.parseInt(request.getParameter("stateId"));
		request.setAttribute("stateCities", CacheManager
				.getCitiesByStateId(stateId));
		return mapping.findForward("success");
	}
}
